package com.yanqu.road.server.logic.activity.orewar.condition;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.rank.yrank.UserYRank;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.orewar.OreWarActivityData;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.rank.YRankMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.pb.PlayerPb;

import java.math.BigInteger;

public class CrossOreWarActivityScoreRankCondition extends BaseOreWarActivityRankCondition {

    public CrossOreWarActivityScoreRankCondition(GamePlayer player, OreWarActivityData activityData, ActivityConditionInfo conditionInfo) {
        super(player, activityData, conditionInfo);
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.CrossOreWarAcCoinOutPut.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.CrossOreWarAcCoinOutPut.getValue(), handle);
    }

    @Override
    public boolean isCompleted(GamePlayer player) {
        return false;
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        UnionRiseRankArgs args = (UnionRiseRankArgs) object;
        if(null != args) {
            //虽然结束了，但是还是会自动发奖励，所有以结束展示时间作为不触发的条件
            ActivityInfo activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityData.getActivityId());
            if(null != activityInfo) {
                if (args.getType() == UnionRiseRankArgs.NOTIFY_TYPE_NORMAL) {
                    setValue(getValue().add(args.getValue()));
                    // 重新生成排行榜
                    if(getValue().compareTo(BigInteger.ZERO) > 0 ){
                        int activityId = activityData.getActivityId();
                        long userId = player.getUserId();
                        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, userId);
                        if (groupId == UnionActivityGroupHelper.NO_GROUP_ID) {
                            return;
                        }
                        UserBaseInfo userBaseInfo = PlayerBasePb.parseBaseUserInfo(PlayerPb.parsePlayerBaseTempMsg(player.getUserInfo()).build());

                        UserYRank userYRank = new UserYRank();
                        userYRank.setActivityId(activityId);
                        userYRank.setGroupId(groupId);
                        userYRank.setIdKey(String.valueOf(conditionInfo.getType()));
                        userYRank.setUserId(userId);
                        userYRank.setUserBaseInfo(userBaseInfo);
                        userYRank.setValue(getValue());
                        userYRank.setParam("");
                        userYRank.setUpdateTime(System.currentTimeMillis());
                        YRankMgr.addUserYRankCache(userYRank);
                    }
                }
            }
        }
    }
}
